/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.observer.observable;

import java.util.ArrayList;
import java.util.List;

import com.aeky.observer.observer.Observer;

/**
 * 主题公共父类，所有具体主题继承此类
 * 
 * @author aeky
 */
public class Subject {

    private List<Observer> observers = new ArrayList<Observer>();

    /**
     * 注册观察者
     * 
     * @param observer
     */
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    /**
     * 移出观察者
     * 
     * @param observer
     */
    public void removeObserver(Observer observer) {
        observers.remove(observers.indexOf(observer));
    }

    /**
     * 通知观察者
     */
    public void notifyObservers(Object data) {
        for (Observer observer : observers) {
            observer.update(data);
        }
    }
}
